﻿
using System ;
using System.Configuration ;
using System.ComponentModel ;
using System.IO ;

namespace MskEdit
{
    public class ProgramSettings : ApplicationSettingsBase
    {
        #region Private static data
        
        private static ProgramSettings defaultInstance_ = 
            ApplicationSettingsBase.Synchronized( new ProgramSettings() ) as ProgramSettings ;
        
        #endregion
        
        #region Private event handlers
        
        private void handleSettingsLoaded() {
            if ( ProjectSettingsChanged != null ) 
                ProjectSettingsChanged.Invoke() ;
        }


        private void handleSettingsSaving() {
            if ( ProjectSettingsChanged != null ) 
                ProjectSettingsChanged.Invoke() ;
        }
        
        #endregion
        
        #region Public events

        public delegate void ProjectSettingsChangedHandler() ;
        public event ProjectSettingsChangedHandler ProjectSettingsChanged ;

        public delegate void ShowMskGridLinesChangedHandler( bool doShow ) ;
        public event ShowMskGridLinesChangedHandler ShowMskGridLinesChanged ;
        
        #endregion
        
        #region Public foundation
        
        public ProgramSettings() { 
            this.SettingsLoaded += ( sender, e ) => handleSettingsLoaded() ;
            this.SettingsSaving += ( sender, e ) => handleSettingsSaving() ;
        }


        public static ProgramSettings Default {
            get { return defaultInstance_ ; }
        }
        
        #endregion
        
        #region Current project setting attributes
        
        [Browsable( false )]
        public FileInfo projectsFileInfo 
        { get {
            string fileName = useLiveData 
                ? liveDataProjectsFileName 
                : testDataProjectsFileName ;
                
            string directoryName = useLiveData 
                ? liveDataContentDirectoryName 
                : testDataContentDirectoryName ;
            
            return new FileInfo( directoryName + @"\" + fileName ) ;
        } }


        [Browsable( false )]
        public DirectoryInfo rootDirectoryInfo
        { get {
            string rootDirectoryName = useLiveData 
                ? liveDataRootDirectoryName 
                : testDataRootDirectoryName ;
            
            return new DirectoryInfo( rootDirectoryName ) ;
        } }


        [Browsable( false )]
        public DirectoryInfo projectsContentDirectoryInfo
        { get {
            string directoryName = useLiveData 
                ? liveDataContentDirectoryName 
                : testDataContentDirectoryName ;
            
           return new DirectoryInfo( directoryName ) ;
        } }
        
        #endregion

        #region Project setting attributes for PropertyGrid

        [UserScopedSettingAttribute()]
        [DefaultSettingValueAttribute( @"true" )]
        [DisplayName( "Live data" )]
        [Description( "Is using program configuration for live data if true." )]
        [Category( "\t\t\tMaster setting" )]
        public bool useLiveData {
            get { return ( ( bool )( this["useLiveData"] ) ) ; }
            set { this["useLiveData"] = value ; }
        }


        [UserScopedSettingAttribute()]
        [DefaultSettingValueAttribute( @"PROJECTS.DBF" )]
        [DisplayName( "Live data project file" )]
        [Description( "File name without path to project file." )]
        [Category( "\t\tLive data" )]
        public string liveDataProjectsFileName {
            get { return ( ( string )( this["liveDataProjectsFileName"] ) ) ; }
            set { this["liveDataProjectsFileName"] = value ; }
        }


        [UserScopedSettingAttribute()]
//        [DefaultSettingValueAttribute( @"P:\ELSA\PROJ\" )]
        [DefaultSettingValueAttribute( @"C:\Users\Erik\Documents\SharpDevelop Projects\ELSA\" )]
        [DisplayName( "Live data content directory" )]
        [Description( "Path name project content directory." )]
        [Category( "\t\tLive data" )]
        public string liveDataContentDirectoryName {
            get { return ( ( string )( this["liveDataContentDirectoryName"] ) ) ; }
            set { this["liveDataContentDirectoryName"] = value ; }
        }


        [UserScopedSettingAttribute()]
        [DefaultSettingValueAttribute( @"D:\" )]
        [DisplayName( "Live data cmd root" )]
        [Description( "Path name root directory of CMD file operations." )]
        [Category( "\t\tLive data" )]
        public string liveDataRootDirectoryName {
            get { return ( ( string )( this["liveDataRootDirectoryName"] ) ) ; }
            set { this["liveDataRootDirectoryName"] = value ; }
        }


        [UserScopedSettingAttribute()]
        [DefaultSettingValueAttribute( @"PROJECTS.DBF" )]
        [DisplayName( "Test data project file" )]
        [Description( "File name without path to project file." )]
        [Category( "\tTest data" )]
        public string testDataProjectsFileName {
            get { return ( ( string )( this["testDataProjectsFileName"] ) ) ; }
            set { this["testDataProjectsFileName"] = value ; }
        }


        [UserScopedSettingAttribute()]
        [DefaultSettingValueAttribute( @"..\..\..\PROJ\" )]
        [DisplayName( "Test data content directory" )]
        [Description( "Path name project content directory." )]
        [Category( "\tTest data" )]
        public string testDataContentDirectoryName {
            get { return ( ( string )( this["testDataContentDirectoryName"] ) ) ; }
            set { this["testDataContentDirectoryName"] = value ; }
        }
        
        
        [UserScopedSettingAttribute()]
        [DefaultSettingValueAttribute( @"C:\" )]
        [DisplayName( "Test data cmd root" )]
        [Description( "Path name root directory of CMD file operations." )]
        [Category( "\tTest data" )]
        public string testDataRootDirectoryName {
            get { return ( ( string )( this["testDataRootDirectoryName"] ) ) ; }
            set { this["testDataRootDirectoryName"] = value ; }
        }
        
        
        [UserScopedSettingAttribute()]
        [DefaultSettingValueAttribute( @"false" )]
        [DisplayName( "Grid lines" )]
        [Description( "Show grid lines in MSK panel" )]
        [Category( "Graphics" )]
        public bool showMskGridLines {
        
            get { return ( ( bool )( this["showMskGridLines"] ) ) ; }
            
            set { 
                this["showMskGridLines"] = value ;
                
                if ( ShowMskGridLinesChanged != null )
                    ShowMskGridLinesChanged( value ) ;
            }
        }

        #endregion
    }
}
